from myapp.utils.connection import POOL


def identifiedToJson(kw, mid=''):
    conn = POOL.connection()
    cur = conn.cursor()

    sql = ''
    if mid == '':
        sql = "SELECT verified_type FROM {}_findson".format(kw)
    else:
        # 根据根节点筛选
        sql = "SELECT verified_type FROM {}_findson WHERE rootknot = {}".format(
            kw, mid)
    cur.execute(sql)
    result = {'普通用户': 0, '名人': 0, '政府': 0, '企业': 0, '媒体': 0,
              '校园': 0, '网站': 0, '应用': 0, '团体机构': 0, '达人': 0}
    data = cur.fetchall()

    dict = {"-1": "普通用户", "0": "名人", "1": "政府", "2": "企业", "3": "媒体",
            "4": "校园", "5": "网站", "6": "应用", "7": "团体机构", "200": "达人", "220": "达人"}

    for sub_text in data:

        level = str(sub_text[0])
        identity = dict["-1"]
        try:
            identity = dict[level]
        except Exception:
            pass

        if identity in result.keys():
            result[identity] = result[identity] + 1

    cur.close()

    final = {"data": [{"value": result['普通用户'], "name":"普通用户"},
                      {"value": result['名人'], 'name':'名人'},
                      {'value': result['政府'], 'name':'政府'},
                      {'value': result['企业'], 'name':'企业'},
                      {'value': result['媒体'], 'name':'媒体'},
                      {'value': result['校园'], 'name':'校园'},
                      {'value': result['网站'], 'name':'网站'},
                      {'value': result['应用'], 'name': '应用'},
                      {'value': result['团体机构'], 'name':'团体机构'},
                      {'value': result['达人'], 'name':'达人'}
                      ]}
    return final

if __name__ == "__main__":
    identifiedToJson('双黄连')